import pygame, math, sys 

class Block(pygame.sprite.Sprite):
    def __init__(self, pos, screenSize, image, size, speed = [0,0]):
        pygame.sprite.Sprite.__init__(self, self.containers)
        self.frame = 0
        self.waitCount = 0
        self.font = pygame.font.Font(None, 60)
        self.value = 0
        self.image = pygame.image.load(image)
        self.image = pygame.transform.scale(self.image, size)
        self.rect = self.image.get_rect(centerx=pos[0], centery=pos[1])
        self.screenWidth = screenSize[0]
        self.screenHeight = screenSize[1]
        self.go = True
        self.speed = speed
        self.radius = 100
        self.nodamage = False
        self.living = True
        self.didPlace = True
        self.realy = self.rect.center[1]
        self.realx = self.rect.center[0]
        self.xOffset = 0
        self.yOffset = 0
            
    def place(self, pt):
        self.rect.center = (pt)
    
    def move(self, other):
        if other.xScrolling:
            self.xOffset = other.realx
            self.realx += self.speed[0]
            self.place([self.realx-self.xOffset, self.realy])
        if other.yScrolling:
            self.yOffset = other.realy
            self.realy += self.speed[1]
            self.place([self.realx, self.realy-self.yOffset])
        
    def distToPoint(self, pt):
        x1 = self.rect.center[0]
        x2 = pt[0]
        y1 = self.rect.center[1]
        y2 = pt[1]
        return math.sqrt(((x2-x1)**2)+((y2-y1)**2))
        return True

        
    def lavaCollide(self, other):         
        self.slow = True 
        self.life -= 1
          
    def keyCollide(self, other):
        if (self.rect.right > other.rect.left 
            and self.rect.left < other.rect.right):
            if (self.rect.bottom > other.rect.top and 
                self.rect.top < other.rect.bottom): 
                if other.key > 0:
                    self.living = False
                
                other.speed[0] = other.speed[0] * -.8
                other.speed[1] = other.speed[1] * -.8
                
                other.move()
                other.move()
                
                other.speed[0] = other.speed[0] = 0
                other.speed[1] = other.speed[1] = 0
                    
    def rocketCollide(self, other):
        if (self.rect.right > other.rect.left 
            and self.rect.left < other.rect.right):
            if (self.rect.bottom > other.rect.top and 
                self.rect.top < other.rect.bottom): 
                    other.canAnimate = True
    
    def enemyCollide(self, other):
        other.onGround = False
        if (self.rect.bottom > other.rect.top 
            and self.rect.top + 3 < other.rect.bottom):
            if (self.rect.right > other.rect.left 
                and self.rect.left < other.rect.right):
                    other.fall()
                    other.speed[0] = other.speed[0] * -.8
                    other.speed[1] = other.speed[1] * -.8
                    
                    other.move()
                    other.move()
                    
                    other.speed[0] = other.speed[0] = 0
                    other.speed[1] = other.speed[1] = 0
                    
        if (self.rect.right -5 > other.rect.left 
            and self.rect.left +5 < other.rect.right):
            if (self.rect.bottom  - 48 > other.rect.top and 
                self.rect.top < other.rect.bottom):
                    other.onGround = True
                    other.rect.bottom = self.rect.top
                    other.canJump = 0
                    other.startFall = True
                    other.justJump = True
                    self.noScroll = True
                    
    def playerDetect(self, other):
        if self.go == True:
                self.go = False
                return True
        return False
    
    def update(*args):
        self = args[0]
        player = args[1]
        self.move(player)
        
    def keySpawn(self):
        if self.go == True:
           self.go = False
           return True
        return False
    
    def playerKeyCollide(self, other):
        if (self.rect.right > other.rect.left 
            and self.rect.left < other.rect.right):
            if (self.rect.bottom > other.rect.top and 
                self.rect.top < other.rect.bottom):   
                self.living = False
                other.key += 1
                
    def dore (self,other):
        if not self.noScroll:
            if (self.rect.right > other.rect.left 
                and self.rect.left < other.rect.right):   
                    other.key += 1
                    self.dore = (["rsc/imgs/block/keyhole2 - Copy","rsc/imgs/block/keyhole2 - Copy2","rsc/imgs/block/keyhole open"])
                    other.key - 1
                    
#    def fall(self, other):
#        if (self.rect.right > other.rect.left+1 
#            and self.rect.left < other.rect.right+1):
#            if (self.rect.bottom > other.rect.top+1 and 
#                self.rect.top < other.rect.bottom+1):
#                    self.speed[0] = self.speed[0] * 0
#                    self.speed[1] = self.speed[1] * -5
#                    if (self.rect.top < 0 
#                        or self.rect.bottom >self.screenHeight):
#                        self.living = False  
        